package com.fdb.efp.nls.api.rest;

import com.fdb.basic.framework.core.constant.DataRuleType;
import com.fdb.basic.framework.core.constant.PubConstant;
import com.fdb.basic.framework.core.constant.ResponseData;
import com.fdb.basic.framework.web.controller.AbstractController;
import com.fdb.efp.e4a.service.facade.SRoleDataRuleService;
import com.fdb.efp.e4a.service.facade.SRuleSrcService;
import com.fdb.efp.e4a.service.vo.SRuleSrcVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api")
public class SRuleSrcController extends AbstractController  {
	
	@Autowired
	@Qualifier("sRuleSrcService")
	private SRuleSrcService sRuleSrcService;

	@Autowired
	@Qualifier("sRoleDataRuleService")
	private SRoleDataRuleService sRoleDataRuleService;

	/**
	 * 列表数据查询
	 * @param page
	 * @param size
	 * @return
	 */
	@RequestMapping(value="/s/rule/srcs", method=RequestMethod.POST)
	public @ResponseBody ResponseData<List<SRuleSrcVO>> queryAll(@RequestBody SRuleSrcVO vo) {		
		// 根据用户角色获取用户权限方案
		List<SRuleSrcVO> outputVo = null;
		Map<String,String> dataRuleTypeMap = sRoleDataRuleService.getDataRuleType(httpRequest.getRequestURI(), getUserRoles());
		vo.setPrdCodebyBaseInfo(dataRuleTypeMap == null ? null:dataRuleTypeMap.get(PubConstant.PRD_CODE));
		DataRuleType dataRuleType = DataRuleType.getDataRuleType(dataRuleTypeMap == null ? null:dataRuleTypeMap.get(PubConstant.PERMIS_TYPE));
		setUserInfoToVO(vo);
		// 根据权限方案调用不同的方案进行数据查询
		switch (dataRuleType) {
		case Owner:
			outputVo = sRuleSrcService.queryAllOwner(vo);
			break;
		case CurrOrg:
			outputVo = sRuleSrcService.queryAllCurrOrg(vo);
			break;
		case CurrDownOrg:
			outputVo = sRuleSrcService.queryAllCurrDownOrg(vo);
			break;
		default:
			outputVo = sRuleSrcService.queryAllOwner(vo);
			break;
		}
		return getResponseData(outputVo);
		
	}
	
	@RequestMapping(value="/s/rule/srcs/nonpers", method=RequestMethod.POST)
	public @ResponseBody ResponseData<List<SRuleSrcVO>> querySRuleSrcAll(@RequestBody SRuleSrcVO vo) {		
		setUserInfoToVO(vo);
		List<SRuleSrcVO> outputVo = this.sRuleSrcService.querySRuleSrcAll(vo);
		return getResponseData(outputVo);
	}
	
	/**
	 * 根据主键查询详情
	 * @return
	 */
	@RequestMapping(value="/s/rule/src/{ruleId}", method=RequestMethod.GET)
	public @ResponseBody ResponseData<SRuleSrcVO> queryByPk(@PathVariable("ruleId") String ruleId) {		
		SRuleSrcVO inVo = new SRuleSrcVO();
				inVo.setRuleId(ruleId);
		SRuleSrcVO outputVo = sRuleSrcService.queryByPk(inVo);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 根据主键删除信息
	 * @param sRuleSrc
	 * @return
	 */
	@RequestMapping(value="/s/rule/src", method=RequestMethod.DELETE)
	public @ResponseBody ResponseData<Integer> deleteByPk(@RequestBody SRuleSrcVO inSRuleSrcVo) {		
		int outputVo = sRuleSrcService.deleteByPk(inSRuleSrcVo);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 根据主键更新信息
	 * @param inSRuleSrcVo
	 * @return
	 */
	@RequestMapping(value="/s/rule/src", method=RequestMethod.PUT)
	public @ResponseBody ResponseData<Integer> updateByPk(@RequestBody SRuleSrcVO inSRuleSrcVo) {		
		int outputVo = sRuleSrcService.updateByPk(inSRuleSrcVo);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 新增数据
	 * @param inSRuleSrcVo
	 * @return
	 */
	@RequestMapping(value="/s/rule/src", method=RequestMethod.POST)
	public @ResponseBody ResponseData<Integer> insertSRuleSrc(@RequestBody SRuleSrcVO inSRuleSrcVo) {
		int outputVo = sRuleSrcService.insertSRuleSrc(inSRuleSrcVo);
		return getResponseData(outputVo);
		
	}
}
